package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.RideCardUser;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.service.GeoService;
import com.tbit.uqbike.client.service.RideCardUserService;
import com.tbit.uqbike.client.service.TokenService;
import com.tbit.uqbike.client.util.Assert;
import com.tbit.uqbike.client.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 骑行卡用户购买
 *
 * @author Leon
 * 2019年9月24日 下午5:28:46
 */
@Controller
@RequestMapping("/rideCardUser")
public class RideCardUserController {
    @Autowired
    private RideCardUserService rideCardUserService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private GeoService geoService;


    /**
     * 分页查询用户骑行卡购买记录
     *
     * @param accountId   品牌id
     * @param lo          经度
     * @param la          纬度
     * @param mapType     地图类型
     * @param adAccountId 区域id
     * @param token       令牌
     * @param rowCount    每页大小
     * @param pageNO      页码数
     * @param session     会话
     * @param response    响应
     * @ignoreParams request
     */
    @RequestMapping("/queryPage")
    public void queryPage(Integer accountId, Double lo, Double la, Integer mapType, Integer adAccountId, String token, Integer rowCount, Integer pageNO,
                          HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            User user = tokenService.getUser(token);

            /*默认值*/
            if (rowCount == null || rowCount < 1) {
                rowCount = 10;
            }
            if (pageNO == null || pageNO < 1) {
                pageNO = 1;
            }


            //Assert.notNull(adAccountId, "-10001", "品牌id不能为空");
            if (adAccountId == null) {
                Assert.notNull(lo, "-2001", "经度不能为空");
                Assert.notNull(la, "-2002", "纬度不能为空");
                Integer agentId = geoService.getBelongAccount(accountId, lo, la, mapType);
                adAccountId = agentId;
            }

            List<RideCardUser> rideCards = new ArrayList<>();
            Integer count = 0;
            if (adAccountId != null) {
                /**触发骑行卡是否有效更新*/
                rideCardUserService.getRideVaildCard(user, adAccountId, null);
                /**兼容查询所有骑行卡*/
                adAccountId = null;
                rideCards = rideCardUserService.queryPage(user.getUserId(), adAccountId, pageNO, rowCount);
                rideCards.forEach(rideCardUser -> {
                    rideCardUser.setFeeName(rideCardUser.getFeeName() == null ? "全车型" : rideCardUser.getFeeName());
                });
                /*获取总数*/
                count = rideCardUserService.queryCount(user.getUserId(), adAccountId);

            }
			/*List<RideCardUser>	rideCards=rideCardUserService.queryPage(user.getUserId(),adAccountId,pageNO,rowCount);
			Integer count=rideCardUserService.queryCount(user.getUserId(),adAccountId);*/
            map.put("total", count);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, rideCards);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }

}